<?php defined('_JEXEC') or die('Restricted access'); 
mb_internal_encoding("UTF-8");
?>
<h1>
	<?php 
	JText::printf("BILLING"); 
	echo " ".$this->eventGroup->description;
	?>
</h1>
<?php 

if (!is_null($this->eventId)) {
	die ("Not to be shown for single events.");
}
else if (!$this->eventGroup->allEventsClosed) {
	die ("Only to be shown if all events are closed.");
}
else if (!$this->eventGroup->allEventsBilling) {
	?>
	<form action="<?php echo $this->baseurl ?>/index.php" method="post">
		<button type="submit"><?php JText::printf("SET_ALL_STATUS_BILLING"); ?></button>
		<input type="hidden" name="option" value="<?php echo $this->option; ?>" />
		<input type="hidden" name="view" value="signupreport" />
		<input type="hidden" name="task" value="save" />
		<input type="hidden" name="status" value="<?php echo CMeals::STATUS_BILLING; ?>" />
		<input type="hidden" name="group_id" value="<?php echo $this->eventGroup->id; ?>" />
		<input type="hidden" name="tpl" value="excel" />
	</form>
	<?php
}
else {

	$fileName = $this->eventGroup->id.".csv";
	$suggestedDownloadFileName = JText::_("BILLING")." ".$this->eventGroup->description.".csv";
	// Replace \:/*?|"<> with - (escape = single backslash = "\\")
	$regExp = "/[\\\\\:\/\\*\\?\\|\"<>]+/";
	$fileName = preg_replace($regExp, "-", $fileName);
	$suggestedDownloadFileName = preg_replace($regExp, "-", $suggestedDownloadFileName);
	$COMP_REL_FILEPATH = "excel".DS.$fileName;
	$ABS_FILEPATH = JPATH_COMPONENT_SITE.DS.$COMP_REL_FILEPATH;
	
	if (!file_exists($ABS_FILEPATH)) {
		
		function xlsText($text) {
			return JText::_($text);
		}
		
		$csvData = "";
		
		function appendRow($dataArray, &$csvData) {
			static $rowWidth = -1;
			if ($rowWidth == -1) {
				$rowWidth = count($dataArray);
			}
			else {
				if ($rowWidth != count($dataArray)) die ("Incorrect number of elements (expected ".$rowWidth.") in ".print_r($dataArray, true));
			}
			$csvData .= implode(";", $dataArray).";\n";
		}
		
		// Create excel signup data as CSV
		
		// Write headers (two first lines)
		$dataArray1 = array();
		$dataArray1[] = $this->eventGroup->description;
		$dataArray2 = array();
		$dataArray2[] = "";
		foreach ($this->eventList as $event) {
			foreach ($event->categoryList as $category) {
				// Weekday in line 1, and category in line 2
				$weekday = date("l", strtotime($event->date));
				$dataArray1[] = xlsText($weekday);
				$dataArray2[] = xlsText($category->display_name);
			}
		}
		appendRow($dataArray1, $csvData);
		appendRow($dataArray2, $csvData);
		
		// Write data
		foreach ($this->memberList as $member) {
			$dataArray = array();
			// Member name
			$dataArray[] = $member->name;
			foreach ($this->eventList as $event) {
				foreach ($event->categoryList as $category) {
					// Number of participants
					$dataArray[] = $category->participantsList[$member->id];
				}
			}
			appendRow($dataArray, $csvData);
		}
		
		// Save file
		$fh = fopen($ABS_FILEPATH, "w") or die("Cannot write to file: ".$ABS_FILEPATH);
		fwrite($fh, iconv("UTF-8", "ISO-8859-1//TRANSLIT", $csvData));
		fclose($fh);
	}
	
	if (file_exists($ABS_FILEPATH)) {
		$this->cmealsMenu->addSeparator();
		$this->cmealsMenu->addLink("EXCEL_BILLING_REPORT", $this->baseurl."/components/com_communalmeals/download.php?file=".urlencode($COMP_REL_FILEPATH)."&name=".urlencode($suggestedDownloadFileName));
	}
	else {
		CMeals::getErrorHandler()->handleError("FILE_NOT_FOUND_ERROR", $ABS_FILEPATH);
	}
}
// Menu
echo $this->cmealsMenu->toString();

if ($this->eventGroup->allEventsArchive) {
	echo "<br/>&nbsp;<p><b>".JText::_("ALL_EVENTS_STATUS_ARCHIVE").".</b></p><br/>&nbsp;";
}
else if ($this->eventGroup->allEventsBilling) {
	echo "<br/>&nbsp;<p><b>".JText::_("ALL_EVENTS_STATUS_BILLING").".</b></p><br/>&nbsp;";
}

?>
